package com.bto.leasetest.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

public class JsonRequestHelper {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static Map<String, Object> getJsonBody(HttpServletRequest request) throws IOException {
        StringBuilder jsonBody = new StringBuilder();
        try (ServletInputStream inputStream = request.getInputStream();
             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                jsonBody.append(line);
            }
        }

        // 将JSON字符串转换为Map对象
        @SuppressWarnings("unchecked")
        Map<String, Object> bodyMap = objectMapper.readValue(jsonBody.toString(), Map.class);
        return bodyMap;
    }
}
